/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package helper;

import java.io.*;
import java.util.Vector;
import javax.microedition.lcdui.*;
import model.ItemMenu;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;
import org.xmlpull.v1.XmlPullParserException;
//
/**
 *
 * @author Hallucinogen
 */
public class Util {

//    public static String getFOLDER_PATH() {
//        throw new UnsupportedOperationException("Not yet implemented");
//    }

    public Util() {
    }

    public static Util getInstance() {
        if (instance == null) {
            instance = new Util();
        }

        return instance;
    }

    /**
     * Scale the input image to size width and height preserving the size ratio
     * @param img
     * @param width
     * @param height
     * @return
     */
    public Image getScaledImage(Image img, int width, int height) {
        Image res = Image.createImage(width, height);
        Graphics g = res.getGraphics();

        int sourceWidth = img.getWidth();
        int sourceHeight = img.getHeight();
        float scaleFactor = 1;


        if (width <= 0) {
            scaleFactor = height / sourceHeight;
        } else if (height <= 0) {
            scaleFactor = width / sourceWidth;
        } else {
            float sf1 = (float) height / (float) sourceHeight;
            float sf2 = (float) width / (float) sourceWidth;

            scaleFactor = sf1 < sf2 ? sf1 : sf2;
        }

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                g.setClip(x, y, 1, 1);
                int dx = (int) (x / scaleFactor);
                int dy = (int) (y / scaleFactor);

                g.drawImage(img, x - dx, y - dy, Graphics.TOP | Graphics.LEFT);
            }
        }

        return res;
    }

    /**
     * Read file content and return it as string.
     * @param file The file that will be read.
     * @return A string containing the contents of the file.
     */
    public String readFile(String file) {
        //Read file string
        InputStream text = getClass().getResourceAsStream(file);

        //Initialize vairable
        StringBuffer buffer = new StringBuffer();
        int character = 0;

        try {
            //Get character
            character = text.read();

            //While character is valid
            while (character != -1) {
                //Add it to buffer
                buffer.append((char) character);
                character = text.read();
            }

            //Return the buffer
            return buffer.toString();

        } catch (Exception ex) {
        }

        //Return no string
        return null;
    }

     public String getBackgroung( String type) throws Exception {

        String method = "getBackground";
        SoapObject client = new SoapObject(Util.targetName, method);
       
        client.addProperty("type", type);

        HttpTransport transport = new HttpTransport(Util.URL);

        // Creating the Soap Envelope
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut = client;

        // Call the WebService
        try {
            transport.call(method, envelope);
        } catch (XmlPullParserException io) {
            System.err.println(io);
        }

        System.out.println("Hasil :" + envelope.getResponse());

        return envelope.getResponse().toString();
    }

     public String getMenu(int parentId, String type) throws Exception {

        String method = "getMenu";
        SoapObject client = new SoapObject(Util.targetName, method);
        client.addProperty("parent_id", String.valueOf(parentId));
        client.addProperty("type", type);

        HttpTransport transport = new HttpTransport(Util.URL);

        // Creating the Soap Envelope
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut = client;

        // Call the WebService
        try {
            transport.call(method, envelope);
        } catch (XmlPullParserException io) {
            System.err.println(io);
        }

        System.out.println("Hasil :" + envelope.getResponse());

        return envelope.getResponse().toString();
    }


     public String getMenuByParentId(int parentId) throws Exception {

        String method = "getMenuByParentId";
        SoapObject client = new SoapObject(Util.targetName, method);
        client.addProperty("parent_id", String.valueOf(parentId));

        HttpTransport transport = new HttpTransport(Util.URL);

        // Creating the Soap Envelope
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut = client;

        // Call the WebService
        try {
            transport.call(method, envelope);
        } catch (XmlPullParserException io) {
            System.err.println(io);
        }

        System.out.println("getMenuByParentId Hasil :" + envelope.getResponse());

        return envelope.getResponse().toString();
    }

     public String getSms(int menuId) throws Exception {

        String method = "getSMS";
        SoapObject client = new SoapObject(Util.targetName, method);
        client.addProperty("menu_id", String.valueOf(menuId));

        HttpTransport transport = new HttpTransport(Util.URL);

        // Creating the Soap Envelope
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut = client;

        // Call the WebService
        try {
            transport.call(method, envelope);
        } catch (XmlPullParserException io) {
            System.err.println(io);
        }
//        System.out.println("getSms Hasil :" + envelope.getResponse());

        return envelope.getResponse().toString();
    }


    public static String[] split(String original, String separator) {
        Vector nodes = new Vector();
        //System.out.println("split start...................");
        // Parse nodes into vector
        int index = original.indexOf(separator);
        while (index >= 0) {
            nodes.addElement(original.substring(0, index));
            original = original.substring(index + separator.length());
            index = original.indexOf(separator);
        }
        // Get the last node
        nodes.addElement(original);

        // Create splitted string array
        String[] result = new String[nodes.size()];
        if (nodes.size() > 0) {
            for (int loop = 0; loop < nodes.size(); loop++) {
                result[loop] = (String) nodes.elementAt(loop);
//                System.out.println(result[loop]);
            }

        }
        return result;
    }


    public static ItemMenu[] sortingMenu(ItemMenu[] item){
        ItemMenu temp = null;
        for (int i = 0 ; i< item.length-1; i++){
            if (item[i].getMenuName().length() < item[i+1].getMenuName().length()){
                temp = item [i];
                item[i] = item [i+1];
                item[i+1] = temp;
            }
        }
        return item;
    }

    public static int textLengh(ItemMenu [] items){
        int lenght=0;
        for (int i = 0 ; i< items.length-1; i++){
            if(items[i].getMenuName().length() > lenght){
                lenght = items[i].getMenuName().length();
            }
        }
        return lenght;
    }
    private static Util instance;

//    public static String URL = "http://localhost:8080/axis2/services/MobileServices";
//    public static String URL = "http://192.168.1.127:8080/axis2/services/MobileServices";
    public static String URL = "http://soap.beavers-studio.com/axis2/services/MobileServices";
//    acces name
//    public static String URL = "http://localhost/webservice/mobile_service.php";
    public static String targetName ="http://services.zingmobile.com";

    public static int PARENT_MENU = -1;
   
}
